package com.haotunet.app.goldtreasure.activity;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.db.DBOpenHelper;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.net.UrlUtil;
import com.haotunet.app.goldtreasure.other.Interface;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.ApkUpdateControl;
import com.haotunet.app.goldtreasure.utils.ShareStore;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.utils.Utils;
import com.haotunet.app.goldtreasure.views.TitleView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class SettingActivity extends BaseActivity {
    @InjectView(R.id.title_view)
    TitleView titleView;
    @InjectView(R.id.tv_realname_txt)
    TextView tvRealnameTxt;
    @InjectView(R.id.txt_current_version)
    TextView txtCurrentVersion;
    @InjectView(R.id.iv_head_image)
    CircleImageView ivHeadImage;
    @InjectView(R.id.iv_auth_image)
    ImageView ivAuthImage;
    private ShareStore mStore;
    private DBOpenHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.inject(this);
        mStore = new ShareStore(this);
        helper = new DBOpenHelper(SettingActivity.this);
        initView();
    }

    @SuppressWarnings("deprecation")
    private void initView() {
        titleView.getTitletextView().setVisibility(View.VISIBLE);
        titleView.getTitletextView().setText(getString(R.string.action_settings));
        titleView.getBackView().setVisibility(View.VISIBLE);
        titleView.getBackView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if ("".equals(helper.getRealName())) {
            tvRealnameTxt.setText(Utils.hideMidFourPhoneNum(helper.getMobile()) + "身份信息未认证");
            ivAuthImage.setBackground(getResources().getDrawable(R.drawable.weirenzheng));
        } else {
            tvRealnameTxt.setText(Utils.hideMidFourPhoneNum(helper.getMobile()) + "身份信息已认证");
            ivAuthImage.setBackground(getResources().getDrawable(R.drawable.yirenzheng));
        }
        Glide.with(SettingActivity.this).load(mStore.getString(Strs.HEAD_IMAGE, "")).error(R.drawable.default_head_image).into(ivHeadImage);
        txtCurrentVersion.setText("V" + getAppVersion());

    }

    private void loginout() {
        ActionUtil actionUtil = new ActionUtil(SettingActivity.this);
        actionUtil.logOut();
        actionUtil.setOnPassBackListener(new Interface.OnPassBackListener() {
            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()){
                    return;
                }
                if (model == null) {
                    UIHelper.showToast(SettingActivity.this, getString(R.string.rank_fail));
                    return;
                }
                if (model.getErrorMsg() != null) {
                    if (model.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(SettingActivity.this, getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(SettingActivity.this, model.getErrorMsg());
                    }
                } else {
                    SettingActivity.this.setResult(RESULT_OK);
                    mStore.putBoolean(Strs.IS_LOGIN, false);
                    mStore.putString(Strs.TOKEN, "");
                    mStore.commit();
                    helper.deleteValue(helper.getMobile());
                    finish();
                }
            }
        });

    }

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        super.onActivityResult(arg0, arg1, arg2);
        if (arg0 == 1 && arg1 == RESULT_OK) {
            loginout();
        }
    }

    private String getAppVersion() {
        PackageManager manager = this.getPackageManager();
        PackageInfo info;
        String version = null;
        try {
            info = manager.getPackageInfo(this.getPackageName(), 0);
            version = info.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return version;

    }


    @OnClick({R.id.rl_realname_layout, R.id.rl_bank_card, R.id.rl_change_pwd, R.id.rl_feedback, R.id.rl_about_us, R.id.rl_service_agreement, R.id.rl_current_version, R.id.btn_login_out})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_realname_layout:
                ActivityUtils.gotoPersonalInfo(SettingActivity.this);
                break;
            case R.id.rl_bank_card:
                ActivityUtils.gotoMyBankCard(SettingActivity.this);
                break;
            case R.id.rl_change_pwd:
                Intent intent = new Intent();
                intent.setClass(SettingActivity.this, ManagePwdActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.rl_feedback:
                Intent opinionIntent = new Intent(SettingActivity.this, FeedBackActivity.class);
                startActivity(opinionIntent);
                break;
            case R.id.rl_about_us:
                ActivityUtils.gotoDynamicDetail(SettingActivity.this, UrlUtil.ABOUT_US, 6);
                break;
            case R.id.rl_service_agreement:
                ActivityUtils.gotoDynamicDetail(SettingActivity.this, UrlUtil.SERVICE_PROTOCOL, 3);
                break;
            case R.id.rl_current_version:
                ApkUpdateControl.getInstance().checkVersion(SettingActivity.this, 2);
                break;
            case R.id.btn_login_out:
                loginout();
                break;
        }
    }
}
